<?php
$this->breadcrumbs=array(
	'Admin Panel'=>$this->createUrl( '/adminpanel'),
	'Rental Properties'=>array('index'),
	$model->id,
);

$this->menu=array(
	array('label'=>'List Rentalproperty', 'url'=>array('index')),
	array('label'=>'Create Rentalproperty', 'url'=>array('create')),
	array('label'=>'Update Rentalproperty', 'url'=>array('update', 'id'=>$model->id)),
	array('label'=>'Delete Rentalproperty', 'url'=>'#', 'linkOptions'=>array('submit'=>array('delete','id'=>$model->id),'confirm'=>'Are you sure you want to delete this item?')),
	array('label'=>'Manage Rentalproperty', 'url'=>array('admin')),
	array('label'=>'Add Income', 'url'=>array('/adminpanel/payments/create','pid'=>$model->id,'typ'=>'Income', 'rurl'=>Yii::app()->request->requestUri)),
	array('label'=>'Add Expense', 'url'=>array('/adminpanel/payments/create','pid'=>$model->id,'typ'=>'Expense', 'rurl'=>Yii::app()->request->requestUri))
	
);

 

Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl.'/assets/js/tabs.js');
?>
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/tabs.css" media="screen, projection" />

<h1><?php echo $model->AddressOne; ?></h1>

<ul class="tabs">
    <li><a href="#tab1">Overview</a></li>
    <li><a href="#tab2">Rental Units</a></li>
    <li><a href="#tab3">Payment Information</a></li>
    <li><a href="#tab4">Address Information</a></li>
    <li class="<?php echo $active ?>"><a href="#tab5">Bookkeeping</a></li>
</ul>
<?php if ($active == "active") {?>
<script>
$(document).ready(function() {
	$(".tab_content").hide();
	var lastactive=$('ul.tabs li.active:last');
	$("ul.tabs li").removeClass("active");
	$(lastactive).addClass("active");
	var activeTab = $(lastactive).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
	$(activeTab).fadeIn(); 
	
});
</script>
<?php }?>
<div class="tab_container">

<div id="tab1" class="tab_content">
<?php if(!isset($_GET['year']))
$year='2011';
else 
$year=$_GET['year'];?>
		<div class="grid-view">
			<table class="items" border="0">

				<tr>
					<th><?php echo CHtml::link(CHtml::encode($year-1),
                                 array('/adminpanel/rentalproperty/view','id'=>$model->id, 'year'=>$year-1))?></th>
					<td width="80%" bgcolor="#FFFFFF" bordercolor="#FFFFFF"></td>
					<th><?php echo CHtml::link(CHtml::encode($year+1),
                                 array('/adminpanel/rentalproperty/view','id'=>$model->id, 'year'=>$year+1))?></th>
				</tr>
			</table>
		</div>

<?php

$uts=array();
$unts=Rentalunit::model()->findAll('idRentalProperty=:prty',array(':prty' =>$model->id) );
foreach ($unts as $i=>$unt)
{
	$uts[$i]['Name']=$unt->UnitNumber;
	$uts[$i]['payments']=array(0,0,0,0,0,0,0,0,0,0,0,0);
}
//print_r($uts);
$pymnt=Payments::model()->with(
    array(
    'idIncomeType0'=>array(
          'select'=>'Category',
    	  'condition'=>'idIncomeType0.Category NOT IN ( \'Security Deposits\',\'Fee\')',	
    	  
    ),
    
    'idOccupancy0'=>array(
          'select'=>'id,idRentalProperty',
    	'condition'=>'idOccupancy0.idRentalProperty='.$model->id,	
    ),
    'idOccupancy0.idRentalUnit0'=>array(
          'select'=>'UnitNumber',
    ),
    
)
   )->findAll(array ('group'=>'paiddate','having'=>'DATE_FORMAT(paiddate,"%Y")='.$year));

$months=array('Jan', 'Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
$expense=array(0,0,0,0,0,0,0,0,0,0,0,0);
$income=array(0,0,0,0,0,0,0,0,0,0,0,0);
$payment=array(0,0,0,0,0,0,0,0,0,0,0,0);
$net=array(0,0,0,0,0,0,0,0,0,0,0,0);

foreach ($pymnt as $row)
{
	$mnt = new DateTime($row->PaidDate);
	
	$mnt=ltrim($mnt->format('m')-1, '0');
	//echo $row->Amount.'<br>';
	//echo $row->PaidDate."--".$mnt."<br>";
	if ($row->idIncomeType0->Category == 'Income')
		$income[$mnt]+= $row->Amount;
		
	if ($row->idIncomeType0->Category == 'Expense' )
		$expense[$mnt]+= $row->Amount;
		
	if ($row->idIncomeType0->Category == 'Payment')
	{
		$payment[$mnt]+= $row->Amount;
		foreach ($uts as $i=>$ut)
		{
			//echo  "_>". $ut['Name'];
		if($ut['Name']==$row->idOccupancy0->idRentalUnit0->UnitNumber)
		{
			$uts[$i]['payments'][$mnt]+= $row->Amount;
			//echo $mnt.'=='.$uts[$i]['payments'][$mnt].'<br>';
		}
			
			//echo $ut['payments'][$mnt];
		}	
	}
	    //$tbl[$months[$mnt]][$row->idOccupancy0->idRentalUnit0->UnitNumber]+= $row->Amount;
}
//print_r($uts);
//foreach ($pymnt as $row)
//{
//	$mnt = new DateTime($row->PaidDate);
	
//	$mnt=ltrim($mnt->format('m')-1, '0');
foreach ($months as $mnt=>$m)
{
			$payment[$mnt]+=$income[$mnt];
			$net[$mnt]=$payment[$mnt]-$expense[$mnt];
			//echo $mnt.'--'.'+'.$income[$mnt].'='.$payments[$mnt].'--'.$net[$mnt].'<br>';
		
		
}

//print_r($payments);

$this->Widget('ext.highcharts.HighchartsWidget', array(
   'options'=>array(
      'title' => array('text' => $year.' Operating Income'),
      'xAxis' => array(
         'categories' => $months
      ),
      'yAxis' => array(
         'title' => array('text' => 'Dollars ($)')
      ),
      'series' => array(
         array('type'=> 'column','name' => 'Payments', 'data' => $payment),
         array('type'=> 'column','name' => 'Expance', 'data' => $expense),
         array('name' => 'Net Income','data' => $net,
                    
         ),
      ),
      'credits' => array('enabled' => false),
   )
));

?>
		<div class="grid-view">
			<table class="items">

				<tr>
					<th><?php echo $year ?></th>

					<?php foreach ($months as $m=>$mnt){?>
					<th><?php echo $mnt?></th>
					<?php }?>
				</tr>
				<tr>
					<th>Income</th>
					<?php foreach ($months as $m=>$mnt){?>
					<td><?php echo $income[$m]?></td>
					<?php }?>
				</tr>
				<?php foreach ($uts as $ut) {?>
				<tr>
					<th><?php echo $ut['Name']?>
					</th>
					<?php foreach ($months as $m=>$mnt){?>
					<td><?php echo $ut['payments'][$m]?></td>
					<?php }?>
				</tr>
				<?php }?>
				<tr>
					<th>Expense</th>
					<?php foreach ($months as $m=>$mnt){?>
					<td><?php echo $expense[$m]?></td>
					<?php }?>
				</tr>
				<tr>
					<th>Net</th>
					<?php foreach ($months as $m=>$mnt){?>
					<td><?php echo $net[$m]?></td>
					<?php }?>
				</tr>
			</table>
		</div>
	</div>

<div id="tab4" class="tab_content">
<?php $this->widget('zii.widgets.CDetailView', array(
	'data'=>$model,
	'attributes'=>array(
		array(
			'label'=>'Managing Company',
			'type'=>'raw',
			'value' =>CHtml::link(CHtml::encode($model->idManagement0->CompanyName),
                                 array('/adminpanel/management/view','id'=>$model->idManagement0->id)),
		),
		'AddressOne',
		'AddressTwo',
		'City',
		'State',
		'ZipCode',
		'SoftwareCode',
		'EmailAddress',
		array(               // related city displayed as a link
            'label'=>'Active Manager',
            'type'=>'raw',
            'value'=>  (($model->ActiveManager)? CHtml::link(CHtml::encode($model->activeManager->SiteFirstName.' '.$model->activeManager->SiteLastName ),
                                 array('/adminpanel/sitemanager/update','id'=>$model->ActiveManager, 'pid'=>$model->id)).'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.CHtml::link(CHtml::encode('Change'),
                                 array('/adminpanel/sitemanager/create','pid'=>$model->id, 'mid'=>$model->idManagement0->id)):CHtml::link(CHtml::encode('Add'),
                                 array('/adminpanel/sitemanager/create','pid'=>$model->id, 'mid'=>$model->idManagement0->id))),	 
        ),
		'PhoneNumber',
		'NumberOfUnits',
		array(
			'name'=>'ApplicationDocument',
			'type'=>'raw',
			'value' =>CHtml::link(CHtml::encode($model->ApplicationDocument ),Yii::app()->baseUrl . '/assets/uploads/documents/'.$model->ApplicationDocument,array('target'=>'_blank'))
			
		),
		'RulesGracePeriod',
		'LateFee',
		'LateFeeFrequency',
		'NameOfDevelopement',
		array(
			'label'=>'Site Fees Packages ( charge on this property only)',
			'type'=>'raw',
			'value' =>(($model->fees->selectedFeesPack->Amount)? CHtml::link(CHtml::encode($model->fees->selectedFeesPack->Name.' [ $'.$model->fees->selectedFeesPack->Amount.' ]' ),array('/adminpanel/fees/update','id'=>$model->fees->id,'pid'=>$model->id)):CHtml::link(CHtml::encode('Add' ),array('/adminpanel/fees/create','pid'=>$model->id))
			)
		),
	),
)); ?>
 <br /> 
 </div>

<div id="tab2" class="tab_content"> 
<?php 

//print_r();


$this->renderPartial(
	'/rentalunit/_rUnitGrid',
	array(
	'dataProvider'=>$dataProvider,
	'pid'=>$model->id
	)
	); 
	
?>
</div>
<div id="tab3" class="tab_content">
 <?php 

if ($model->paymentinformations){
$this->renderPartial(
	'/paymentinformation/_viewblock',
	array(
	'model'=>$model->paymentinformations,
	'pid'=>$model->id
	)
	); 
}
else
{
echo '<p> <h2>Payment Information </h2>  This Property do not have Payment Infortmation Please '. CHtml::link(CHtml::encode('Add Payment Information' ), array('/adminpanel/paymentinformation/create','pid'=>$model->id,'mid'=>$model->idManagement)).'  here.</p>';
}
?>
</div>
<div id="tab5" class="tab_content">
<?php $this->renderPartial(
	'/payments/_IncomeExpense',
	array(
	'pid'=>$model->id
	)
	); ?>
</div>
</div>